package com.emser.unc.client.view;


import com.emser.unc.client.CloudMobile;
import com.emser.unc.client.presenter.SellerPresenter;
import com.emser.unc.client.resources.GlobalResources;
import com.emser.unc.serializable.SellerDTO;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.dom.client.HasKeyPressHandlers;
import com.google.gwt.event.dom.client.HasScrollHandlers;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.HasData;
import com.google.gwt.view.client.ProvidesKey;
import com.google.gwt.view.client.SingleSelectionModel;
public class SellerView extends Composite implements SellerPresenter.Display {
	private Button btnAdd,btnEdit, btnRemove, btnCustomers, btnProducts;
	private PushButton btnSearch;
	private VerticalPanel verticalPanel;
	private HorizontalPanel hPanel;
	private Label lblPager;	
	private final int PAGE_SIZE = CloudMobile.constants.pageSizeSellers();
	private ScrollPanel scrollable;
	private TextBox searchParam;
	private CellList<SellerDTO> cellList;
	private SingleSelectionModel<SellerDTO> selectionModel;
	private MessageWView messageDelete;
	private BusyIndicatorView busyIndicator = new BusyIndicatorView(CloudMobile.constants.msjWorking());
	ProvidesKey<SellerDTO> keyProvider = new ProvidesKey<SellerDTO>() {
        public Object getKey(SellerDTO item) {
           return (item == null) ? null : item.getUserName();
        }
     };
	private Label name, lastName, address, city, cp, password, userName, telephone;
	private Label serialDevice;
	private int heightCPanel;
	
	public SellerView() {	
		verticalPanel = new VerticalPanel();
		initWidget(verticalPanel);
		verticalPanel.setSize("100%", CloudMobile.get().getHeightPanelCenter()+"px");
		heightCPanel = CloudMobile.get().getHeightPanelCenter() - 40;
		initHPanelTitle();
		initCenterPanel();
	    messageDelete = new MessageWView();
	    messageDelete.getLblMessageW().setText(CloudMobile.constants.msjWarningDelete());
	    messageDelete.hide();
	    busyIndicator.hide();
	}
	
	private void initCenterPanel() {	
		hPanel = new HorizontalPanel();
		initializeCellList();	
		VerticalPanel verticalPanel_1 = new VerticalPanel();
		VerticalPanel verticalPanel_2 = new VerticalPanel();
		verticalPanel_2.setStyleName("gwt-VPanelInfo");
		verticalPanel_2.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		Label lblNewLabel = new Label("Informacion de Vendedor");
		lblNewLabel.setStyleName("gwt-LblTitleInfo");
		verticalPanel_2.add(lblNewLabel);
		lblNewLabel.setSize("380px", "30px");
		verticalPanel_2.setCellHorizontalAlignment(lblNewLabel, HasHorizontalAlignment.ALIGN_CENTER);
		
		Grid grid = new Grid(9, 2);
		grid.setStyleName("gwt-Grid");
		grid.setWidth("380px");
		verticalPanel_2.add(grid);
		verticalPanel_2.setCellHorizontalAlignment(grid, HasHorizontalAlignment.ALIGN_CENTER);
		
		Label lblname = new Label(CloudMobile.constants.name()+":");
		lblname.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		grid.setWidget(0, 0, lblname);
		lblname.setSize("130px", "30px");
		
		name = new Label("");
		name.setStyleName("gwt-FieldLabels");
		grid.setWidget(0, 1, name);
		name.setSize("130px", "30px");
		
		Label lblLastName = new Label(CloudMobile.constants.lastName()+":");
		lblLastName.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		grid.setWidget(1, 0, lblLastName);
		lblLastName.setSize("130px", "30px");
		
		lastName = new Label("");
		lastName.setStyleName("gwt-FieldLabels");
		grid.setWidget(1, 1, lastName);
		lastName.setSize("130px", "30px");
		
		Label lblAddress = new Label(CloudMobile.constants.address()+":");
		lblAddress.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		grid.setWidget(2, 0, lblAddress);
		lblAddress.setSize("130px", "30px");
		
		address = new Label("");
		address.setStyleName("gwt-FieldLabels");
		grid.setWidget(2, 1, address);
		address.setSize("130px", "30px");
		
		Label lblCity = new Label(CloudMobile.constants.city()+":");
		lblCity.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		grid.setWidget(3, 0, lblCity);
		lblCity.setSize("130px", "30px");
		
		city = new Label("");
		city.setStyleName("gwt-FieldLabels");
		grid.setWidget(3, 1, city);
		city.setSize("130px", "30px");
		
		Label lblCP = new Label(CloudMobile.constants.cp()+":");
		lblCP.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		grid.setWidget(4, 0, lblCP);
		lblCP.setSize("130px", "30px");
		
		cp = new Label("");
		cp.setStyleName("gwt-FieldLabels");
		grid.setWidget(4, 1, cp);
		cp.setSize("130px", "30px");
		
		Label lblTelephone = new Label(CloudMobile.constants.telephone()+":");
		lblTelephone.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		grid.setWidget(5, 0, lblTelephone);
		lblTelephone.setSize("130px", "30px");
		
		telephone = new Label("");
		telephone.setStyleName("gwt-FieldLabels");
		grid.setWidget(5, 1, telephone);
		telephone.setSize("130px", "30px");
		
		Label lblUserName = new Label(CloudMobile.constants.userName()+":");
		lblUserName.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		grid.setWidget(6, 0, lblUserName);
		lblUserName.setSize("130px", "30px");
		
		userName = new Label("");
		userName.setStyleName("gwt-FieldLabels");
		grid.setWidget(6, 1, userName);
		userName.setSize("130px", "30px");
		
		Label lblPassword = new Label(CloudMobile.constants.password()+":");
		lblPassword.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		grid.setWidget(7, 0, lblPassword);
		lblPassword.setSize("130px", "30px");
		
		password = new Label("");
		password.setStyleName("gwt-FieldLabels");
		grid.setWidget(7, 1, password);
		password.setSize("130px", "30px");
		
		Label lblSerialDevice = new Label(CloudMobile.constants.serialDevice()+":");
		lblSerialDevice.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		grid.setWidget(8, 0, lblSerialDevice);
		lblSerialDevice.setSize("130px", "30px");
		
		serialDevice = new Label("");
		grid.setWidget(8, 1, serialDevice);
		serialDevice.setSize("130px", "30px");
		setSize("100%", "100%");
		verticalPanel_1.add(verticalPanel_2);
		verticalPanel_2.setSize("400", "340");
		verticalPanel_1.setCellHorizontalAlignment(verticalPanel_2, HasHorizontalAlignment.ALIGN_CENTER);
		
		btnCustomers = new Button(CloudMobile.constants.customers());
		verticalPanel_1.add(btnCustomers);
		verticalPanel_1.setCellHeight(btnCustomers, "30px");
		verticalPanel_1.setCellHorizontalAlignment(btnCustomers, HasHorizontalAlignment.ALIGN_CENTER);
		btnCustomers.setSize("220px", "22px");
		
		btnProducts = new Button(CloudMobile.constants.products());
		verticalPanel_1.add(btnProducts);
		verticalPanel_1.setCellHeight(btnProducts, "30px");
		verticalPanel_1.setCellVerticalAlignment(btnProducts, HasVerticalAlignment.ALIGN_BOTTOM);
		verticalPanel_1.setCellHorizontalAlignment(btnProducts, HasHorizontalAlignment.ALIGN_CENTER);
		btnProducts.setSize("220px", "22px");
		verticalPanel_1.setStyleName("gwt-PanelInfo");
		verticalPanel_1.setSize("300px", "500px");
		hPanel.add(verticalPanel_1);
		hPanel.setCellHeight(verticalPanel_1, "500px");
		hPanel.setCellWidth(verticalPanel_1, "50%");
		hPanel.setCellHorizontalAlignment(verticalPanel_1, HasHorizontalAlignment.ALIGN_LEFT);
		verticalPanel_1.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		verticalPanel_1.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		verticalPanel.add(hPanel);
		hPanel.setSize("100%", heightCPanel+"px");
		verticalPanel.setCellHeight(hPanel, heightCPanel+"px");
		verticalPanel.setCellWidth(hPanel, "100%");
		
	    
	}

	private void initHPanelTitle() {
		HorizontalPanel horizontalPanel = new HorizontalPanel();
		horizontalPanel.setStyleName("gwt-hPanelCenter");
		horizontalPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
	    horizontalPanel.setSize("100%", "40px");
	    verticalPanel.add(horizontalPanel);  
	    verticalPanel.setCellHeight(horizontalPanel, "40px");
	    verticalPanel.setCellWidth(horizontalPanel, "100%");
	    
	    Grid grid = new Grid(1, 3);
	    horizontalPanel.add(grid);
	    horizontalPanel.setCellVerticalAlignment(grid, HasVerticalAlignment.ALIGN_MIDDLE);
	    
	    btnAdd = new Button(CloudMobile.constants.add());
	    grid.setWidget(0, 0, btnAdd);
	    btnAdd.setSize("112", "28");
	    
	    btnEdit = new Button(CloudMobile.constants.edit());
	    grid.setWidget(0, 1, btnEdit);
	    btnEdit.setSize("112", "28");
	    
	    btnRemove = new Button(CloudMobile.constants.remove());
	    grid.setWidget(0, 2, btnRemove);
	    btnRemove.setSize("112", "28");
	    grid.getCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_LEFT);
	    grid.getCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_LEFT);
	    grid.getCellFormatter().setHorizontalAlignment(0, 2, HasHorizontalAlignment.ALIGN_LEFT);
	    
	    Grid grid_1 = new Grid(1, 2);
	    horizontalPanel.add(grid_1);
	    horizontalPanel.setCellVerticalAlignment(grid_1, HasVerticalAlignment.ALIGN_MIDDLE);
	    horizontalPanel.setCellHorizontalAlignment(grid_1, HasHorizontalAlignment.ALIGN_RIGHT);
	    searchParam = new TextBox();
	    grid_1.setWidget(0, 0, searchParam);
	    searchParam.setSize("200px", "28");
	    
	    btnSearch = new PushButton(CloudMobile.constants.search());
	    grid_1.setWidget(0, 1, btnSearch);
	    grid_1.getCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_RIGHT);
	    grid_1.getCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_RIGHT);
	    
	}
	
	public void initializeCellList() {
		VerticalPanel vPanelList = new VerticalPanel();
		vPanelList.setStyleName("gwt-VPanelList");
		vPanelList.setSize("260px", "500px");
	    final GlobalResources resource = GWT.create(GlobalResources.class);
	    scrollable = new ScrollPanel();
		scrollable.setStyleName("gwt-ScrollPanelList");
		vPanelList.add(scrollable);
		vPanelList.setCellHeight(scrollable, "440px");
		vPanelList.setCellWidth(scrollable, "220px");
		scrollable.setSize("220px", "440px");
		CellCustom sellerCell = new CellCustom(resource.getImageSeller());
	    cellList = new CellList<SellerDTO>(sellerCell,keyProvider);
	    cellList.setStyleName("gwt-CellList");
	    cellList.setPageSize(PAGE_SIZE);
	    selectionModel = new SingleSelectionModel<SellerDTO>(keyProvider);
	    cellList.setSelectionModel(selectionModel);
		scrollable.add(cellList);
		lblPager = new Label("0 - 0");
		vPanelList.add(lblPager);
		vPanelList.setCellHeight(lblPager, "20px");
		lblPager.setHeight("20px");
		vPanelList.setCellHorizontalAlignment(lblPager, HasHorizontalAlignment.ALIGN_CENTER);
		scrollable.getElement().setTabIndex(-1);
	   
	    hPanel.setSize("100%-1px", heightCPanel+"px");	
		hPanel.add(vPanelList);
		hPanel.setCellHeight(vPanelList, "500px");
		hPanel.setCellWidth(vPanelList, "50%");
		hPanel.setCellHorizontalAlignment(vPanelList, HasHorizontalAlignment.ALIGN_CENTER); 
		 
	  }

	@Override
	public Widget asWidget() {
		return this;
	}
	
	@Override
	public HasValue<String> getSearchParam() {
		return searchParam;
	}
	
	@Override
	public HasClickHandlers getSearchButton() {
		return btnSearch;
	}

	
	@Override
	public HasClickHandlers getAddButton() {
		return btnAdd;
	}
	
	@Override
	public HasClickHandlers getEditButton() {
		return btnEdit;
	}

	@Override
	public HasClickHandlers getRemoveButton() {
		return btnRemove;
	}
	
	@Override
	public HasScrollHandlers getScrollPanel() {
		return scrollable;
	}
	
	@Override
	public void enableDelete(boolean flag) {
		btnRemove.setEnabled(flag);	
	}

	@Override
	public HasData<SellerDTO> getCellList() {
		return cellList;
	}

	@Override
	public SingleSelectionModel<SellerDTO> getSelectionM() {
		return selectionModel;
	}

	@Override
	public HasText getName() {
		return name;
	}

	@Override
	public HasText getLastName() {
		return lastName;
	}

	@Override
	public HasText getAddress() {
		return address;
	}

	@Override
	public HasText getTelephone() {
		return telephone;
	}

	@Override
	public HasText getUserName() {
		return userName;
	}

	@Override
	public HasText getPassword() {
		return password;
	}

	@Override
	public HasClickHandlers getBtnCustomers() {
		return btnCustomers;
	}

	@Override
	public HasClickHandlers getBtnProducts() {
		return btnProducts;
	}

	@Override
	public void setRange(int start, int lenght, int size) {
		lblPager.setText(start + " - " + lenght + " : " + size);		
	}


	@Override
	public HasClickHandlers getBtnPopUpDCancel() {
		return messageDelete.getBtnCancel();
	}

	@Override
	public HasClickHandlers getBtnPopUpDDelete() {
		return messageDelete.getBtnDelete();
	}

	@Override
	public void showWarning() {
		messageDelete.show();
	}

	@Override
	public void disableWarning() {
		messageDelete.hide();
	}

	@Override
	public void enableBtnAdd(boolean enabled) {
		btnAdd.setEnabled(enabled);
	}

	@Override
	public void enableBtnPopUpDelete(boolean enabled) {
		messageDelete.getBtnDelete().setEnabled(enabled);
	}

	@Override
	public void enableBtnSearch(boolean enabled) {
		btnSearch.setEnabled(enabled);
	}

	@Override
	public void enableBtnEdit(boolean enabled) {
		btnEdit.setEnabled(enabled);	
	}

	@Override
	public HasText getSerialDevice() {
		return serialDevice;
	}

	@Override
	public int getVerticalScrollPosition() {
		return scrollable.getVerticalScrollPosition();
	}

	@Override
	public int getMaximumVerticalScrollPosition() {
		return scrollable.getMaximumVerticalScrollPosition();
	}
	@Override
	public void showBusyIndicator(){
		busyIndicator.show();
	}
	@Override
	public void hideBusyIndicator(){
		busyIndicator.hide();
	}
	@Override
	public HasKeyPressHandlers getSearchTexBox() {
		return searchParam;
	}

	@Override
	public void enableBtnCustomers(boolean enabled) {
		btnCustomers.setEnabled(enabled);	
	}

	@Override
	public void enableBtnProducts(boolean enabled) {
		btnProducts.setEnabled(enabled);
	}

	@Override
	public HasText getZP() {
		return cp;
	}

	@Override
	public HasText getCity() {
		return city;
	}

}
